# Create your views here.

from django.http import HttpResponse
from django.template.loader import get_template  
from django.template import Context  

from django.core.paginator import ObjectPaginator 

from company.product.models import *
from company.utility import * 
            
def product_list(request):
    index =  int ( request.REQUEST.get('page',1) )
    
    products = Product.objects.all().order_by('-pub_date')
    pager = ObjectPaginator(products, 4) 
    
    ps = pager.get_page( index-1 ) 
    interval = range(1, pager.pages + 1)
    data = {'products': ps, 'range': interval, 'index':index, 'sum':pager.hits}
    
    t = get_template('p_list.html')
    
    #html = t.render(Context({'current_date': now})) 
    html = t.render(Context({'data':data}))
    return HttpResponse(html)

def detail(request, product_id):
    p = Product.objects.get(id = product_id)
    
    t = get_template('p_detail.html')
    
    #html = t.render(Context({'current_date': now})) 
    html = t.render(Context({'product':p}))
    return HttpResponse(html)

def news_list( request ):
    index =  int ( request.REQUEST.get('page',1) )
    
    news = New.objects.all().order_by('-pub_date')
    pager = ObjectPaginator(news , 4) 
    
    ns = pager.get_page( index-1 ) 
    interval = range(1, pager.pages + 1)
    data = {'news': ns, 'range': interval, 'index':index, 'sum':pager.hits, 'jo': Jiou()}
    
    t = get_template('news_list.html')
    
    #html = t.render(Context({'current_date': now})) 
    html = t.render(Context({'data':data}))
    return HttpResponse(html)

def news_detail(request, news_id):
    n = New.objects.get(id = news_id)
    
    t = get_template('news_detail.html')
    
    #html = t.render(Context({'current_date': now})) 
    html = t.render(Context({'news':n}))
    return HttpResponse(html)

